<?php

namespace App\Controller\LazyCommon;

use App\Model\LazyCommon\RoleModel;
use App\Model\LazyCommon\UserGroupsModel;
use App\Model\LazyCommon\UserGroupsRoleModel;
use App\Setting\LazyCommon\UserGroupsRoleSetting;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use App\Base\BaseController;
use App\Entity\UserGroupsRole  as Entity;

/**
 * @route("/admin/")
 */
class UserGroupsRoleAdminController extends BaseController
{

    /**
     * @route("UserGroupsRole/add",name="UserGroupsRolePreAdd")
     */
    public function add()
    {
        //通用代码
        $this->setSetting(new UserGroupsRoleSetting($this->getDoctrine()->getManager()));
        return $this->comAdd();
    }

    /**
     * @route("UserGroupsRole/add/do",name="UserGroupsRoleDoAdd")
     *
     * @Method({"GET", "POST"})
     */
    public function doAdd(Request $request)
    {
        //通用代码

        if(!$form = $this->getParamRequired("form")){return $this->msg_out("参数缺失");}
        $groups = $form["groups"];
        $role = $form["role"];

        $UserGroupsRoleModel = new UserGroupsRoleModel($this->getDoctrine());
        $UserGroupsRoleModel->update($groups,$role);
        return $this->json_out(1,"添加成功",$this->generateUrl("UserGroupsRoleLists"));
    }

    /**
     * @route("UserGroupsRole/edit/{id}",name="UserGroupsRolePreEdit")
     */
    public function edit(Entity $entity)
    {
        //私有代码部分
        $UserGroupsRoleModel = new UserGroupsRoleModel($this->getDoctrine());
        $roles = $UserGroupsRoleModel->getRoles4Groups($entity->getGroups());
        $entity->setRole($roles);
        $this->setSetting(new UserGroupsRoleSetting($this->getDoctrine()->getManager()));
        return $this->comEdit($entity);
    }

    /**
     * @route("UserGroupsRole/edit/{id}/do",name="UserGroupsRoleDoEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function doEdit()
    {
        if(!$form = $this->getParamRequired("form")){return $this->msg_out("参数缺失");}
        $groups = $form["groups"];
        $role = $form["role"];
        $UserGroupsRoleModel = new UserGroupsRoleModel($this->getDoctrine());
        $UserGroupsRoleModel->update($groups,$role);
        return $this->json_out(1,"添加成功",$this->generateUrl("UserGroupsRoleLists"));
    }

    /**
     * @route("UserGroupsRole",name="UserGroupsRoleLists")
     */
    public function lists()
    {
        //私有部分
        $UserGroupsModel = new UserGroupsModel($this->getDoctrine());
        $UserGroups = $UserGroupsModel->getAllUserGroupsTree();
        $RoleModel = new RoleModel($this->getDoctrine());
        $Role = $RoleModel->getAllRoleTree();
        $tools = array(
            "groups"    => $UserGroups,
            "role"    => $Role,
            );

        //通用部分
        $this->setSetting(new UserGroupsRoleSetting($this->getDoctrine()->getManager()));
        return $this->comLists($tools);
    }


    /**
     * @route("UserGroupsRole/tools",name="UserGroupsRoleTools")
     */
    public function tools(Request $request)
    {
        $this->setSetting(new UserGroupsRoleSetting($this->getDoctrine()->getManager()));
        return $this->comTools($request);
    }

}
